﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class DbConfigBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly IDbConfigDA DbConfigDA = DataAccess.CreateDbConfigDA();

        private void CreateWH(DbConfigInfo DbConfig)
        {
            string path = ConfigurationManager.AppSettings["CREATEWH_FILE_PATH"];
            if (File.Exists(path + "exec.bat"))
            {
                File.Delete(path + "exec.bat");
            }
            StringBuilder sb = new StringBuilder();
            using (StreamReader sr = File.OpenText(path + "main.bat"))
            {
                string s = string.Empty;
                while ((s = sr.ReadLine()) != null)
                {
                    sb.AppendLine(s.Replace("@DB_LOGIN@", DbConfig.DbLoginID).Replace("@DB_LOGIN_PWD@", DbConfig.DbLoginPasswd));
                }
                File.WriteAllText(path + "exec.bat", sb.ToString(), Encoding.ASCII);
            }
            ProcessStartInfo psi = new ProcessStartInfo("cmd.exe") {
                UseShellExecute = false,
                CreateNoWindow = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                Arguments = @"/c D:\JerryWork\PowerWMS6\Source\PowerWMS6-ServicePort\WMS6.ServiceHost\App_Data\CreateWH\exec.bat",
                WorkingDirectory = @"D:\JerryWork\PowerWMS6\Source\PowerWMS6-ServicePort\WMS6.ServiceHost\App_Data\CreateWH\"
            };
            Process.Start(psi).Close();
        }

        public string Delete(List<string> IDS)
        {
            string temp = string.Empty;
            try
            {
                if (DbConfigDA.DeleteDbConfig(IDS) > 0)
                {
                    for (int i = 0; i < IDS.Count; i++)
                    {
                        temp = temp + IDS[i] + ",";
                    }
                    return temp.Substring(0, temp.Length - 1);
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return DbConfigDA.GetAllDbConfig();
        }

        public object GetDbConfigByDbLoginID(string id)
        {
            return DbConfigDA.GetDbConfigByDbLoginID(id);
        }

        public object GetInfoByID(string id)
        {
            return DbConfigDA.GetDbConfigByID(id);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            DbConfigQueryEntity dbConfigInfo = obj as DbConfigQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(dbConfigInfo.WhDbName))
            {
                sqlWhere.Add(" WH_DB_NAME " + dbConfigInfo.WhDbName);
            }
            if (!string.IsNullOrEmpty(dbConfigInfo.WhDbDesc))
            {
                sqlWhere.Add(" WH_DB_DESC " + dbConfigInfo.WhDbDesc);
            }
            if (!string.IsNullOrEmpty(dbConfigInfo.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + dbConfigInfo.IsActive);
            }
            return DbConfigDA.GetDbConfigByQueryList(sqlWhere, dbConfigInfo);
        }

        public string Insert(object obj)
        {
            string WhID = string.Empty;
            try
            {
                DbConfigInfo DbConfig = obj as DbConfigInfo;
                DbConfig.IsGlobal = "N";
                DbConfig.UpdatedBy = DbConfig.CreatedBy;
                DbConfig.CreatedDate = DateTime.Now;
                DbConfig.UpdatedDate = DateTime.Now;
                if (DbConfigDA.InsertDbConfig(DbConfig) > 0)
                {
                    WhID = DbConfig.WhDbName;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return WhID;
        }

        public string Update(object obj)
        {
            try
            {
                DbConfigInfo DbConfig = obj as DbConfigInfo;
                if (DbConfig.WhDbID == 10)
                {
                    DbConfig.IsGlobal = "Y";
                }
                DbConfig.UpdatedDate = DateTime.Now;
                if (DbConfigDA.UpdateDbConfig(DbConfig) > 0)
                {
                    return DbConfig.WhDbName;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return "";
        }
    }
}

